package org.anti996.lms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.anti996.lms.model.Booking;
import org.anti996.lms.model.Operator;
import org.anti996.lms.model.Warehouse;
import org.anti996.lms.service.BookingService;
import org.anti996.lms.service.OperatorService;
import org.anti996.lms.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 操作员表 前端控制器
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@Slf4j
@RestController
@RequestMapping("/lms/operator")
@CrossOrigin(origins = "*")
public class OperatorController {
    @Autowired
    private OperatorService operatorService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private BookingService bookingService;

    /**
     * 获取操作员列表
     *
     * @return 操作员列表
     */
    @GetMapping
    public List<Operator> operatorsList() {
        return operatorService.list();
    }

    /**
     * 获取操作员分页列表
     *
     * @param no 第no个页面
     * @param sz 每个页面的数据条数
     * @return 分页操作员列表
     */
    @GetMapping("/page/{sz}/{no}")
    public List<Operator> operatorsListPage(@PathVariable(name = "no") int no,
                                            @PathVariable(name = "sz") int sz) {
        Page<Operator> page = operatorService.page(new Page<>(no, sz));
        return page.getRecords();
    }

    @GetMapping("/id/{username}")
    public Integer findIdByUsername(@PathVariable(name = "username") String username) {
        return operatorService.findIdByUsername(username);
    }

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 是否登录成功
     */
    @PostMapping("/login")
    public boolean login(@RequestParam(name = "username") String username,
                         @RequestParam(name = "password") String password) {
        return operatorService.login(username, password);
    }


    /**
     * 根据操作员编号获取操作员信息
     *
     * @param o_id 操作员编号
     * @return 查询到的操作员信息
     */
    @GetMapping("/{id}")
    public Operator operator(@PathVariable(name = "id") int o_id) {
        return operatorService.getById(o_id);
    }

    /**
     * 新增操作员
     *
     * @param operator 操作员信息
     * @return 创建结果
     */
    @PostMapping(consumes = "application/json")
    public boolean insertOperator(@RequestBody Operator operator) {
        return operatorService.save(operator);
    }

    /**
     * 更新操作员
     *
     * @param o_id     操作员编号
     * @param operator 新的操作员信息
     * @return 更新是否成功
     */
    @PutMapping(path = "/{id}", consumes = "application/json")
    public boolean updateOperator(@PathVariable(name = "id") int o_id,
                                  @RequestBody Operator operator) {
        Warehouse flag = warehouseService.getById(operator.getWId());
        if (flag == null) {
            return false;
        } else {
            return operatorService.updateById(operator);
        }
    }

    /**
     * 删除指定操作员编号的数据
     *
     * @param o_id 操作员编号
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean removeOperator(@PathVariable(name = "id") int o_id) {
        return operatorService.removeById(o_id);
    }

    /**
     * 查询所有已到货未签收订单
     *
     * @param w_id 操作员所在仓库编号
     * @return 查询到的信息
     */
    @GetMapping("/getbooking/{wid}")
    public List<Booking> finduncheckout(@PathVariable(name = "wid") int w_id) {
        return bookingService.finduncheckout(w_id);
    }

    @PostMapping("/checkout")
    public boolean checkout(@RequestParam(name = "bnum") int b_num,
                            @RequestParam(name = "oid", defaultValue = "null") Integer o_id,
                            @RequestParam(name = "checkResult") String result,
                            @RequestParam(name = "checkDesc") String desc) {
        Booking booking = bookingService.getById(b_num);
        LocalDateTime localDateTime = LocalDateTime.now();
        booking.setCheckOut(localDateTime);
        if (o_id > 0) {
            booking.setOIdTo(o_id);
        }
        booking.setCheckResult(result);
        booking.setCheckDesc(desc);
        return bookingService.updateById(booking);
    }
}
